/**
 * 
 * <p>
 * https://www.top.iridescent
 * <p>
 * 版权所有，侵权必究！
 */

package top.iridescent.generate.service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import top.iridescent.db.cache.DynamicDataSourceCache;
import top.iridescent.db.service.DynamicDataSourceService;
import top.iridescent.generate.dao.*;
import top.iridescent.generate.utils.GenUtils;
import top.iridescent.generate.utils.PageUtils2;
import top.iridescent.generate.utils.Query;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.io.ByteArrayOutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.zip.ZipOutputStream;

/**
 * 代码生成器
 *
 * @author 
 */
@Service
@Slf4j
public class SysGeneratorService {
    @Autowired
    private GeneratorDao generatorDao;

    @Autowired
    private MySQLGeneratorDao mySQLGeneratorDao;
    @Autowired
    private OracleGeneratorDao oracleGeneratorDao;
    @Autowired
    private SQLServerGeneratorDao sqlServerGeneratorDao;
    @Autowired
    private PostgreSQLGeneratorDao postgreSQLGeneratorDao;

    @Autowired
    private DynamicDataSourceService dataSourceService;

    public PageUtils2 queryList(Query query) {
        String dataSourceName = (String) query.get("dataSourceName");
        if (!ObjectUtils.isEmpty(dataSourceName)) {
            dataSourceService.changeCurrentDataSource(dataSourceName);
        }
        Page<?> page = PageHelper.startPage(query.getPage(), query.getLimit());
        List<Map<String, Object>> list = new ArrayList<>();


        switch (DynamicDataSourceCache.DS_TYPE.get(DynamicDataSourceCache.CURRENT_DS).toLowerCase()) {
            case "mysql":
                list = mySQLGeneratorDao.queryList(query);
                break;
            case "oracle":
                list = oracleGeneratorDao.queryList(query);
                dataSourceService.removeCurrentDataSource();
                break;
        }
        int total = (int) page.getTotal();

        return new PageUtils2(list, total, query.getLimit(), query.getPage());
    }

    public Map<String, String> queryTable(String tableName, GeneratorDao dao) {
        return dao.queryTable(tableName);
    }

    public List<Map<String, String>> queryColumns(String tableName, GeneratorDao dao) {
        return dao.queryColumns(tableName);
    }


    public byte[] generatorCode(String[] tableNames, String dataSourceName) {
        if (!ObjectUtils.isEmpty(dataSourceName)) {
            dataSourceService.changeCurrentDataSource(dataSourceName);
        }
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        ZipOutputStream zip = new ZipOutputStream(outputStream);
        GeneratorDao dao = generatorDao;
        System.out.println(DynamicDataSourceCache.DS_TYPE);
        System.out.println(DynamicDataSourceCache.CURRENT_DS);
        switch (DynamicDataSourceCache.DS_TYPE.get(DynamicDataSourceCache.CURRENT_DS).toLowerCase()) {
            case "mysql":
                log.info("当前数据源为:{} , 数据库类型为{} .",dataSourceName,DynamicDataSourceCache.DS_TYPE.get(DynamicDataSourceCache.CURRENT_DS).toLowerCase());
                dao = mySQLGeneratorDao;
                for (String tableName : tableNames) {
                    //查询表信息
                    Map<String, String> table = queryTable(tableName, mySQLGeneratorDao);
                    //查询列信息
                    List<Map<String, String>> columns = queryColumns(tableName, mySQLGeneratorDao);
                    //生成代码
                    GenUtils.generatorCode(table, columns, zip);
                }
                break;
            case "oracle":
                log.info("当前数据源为:{} , 数据库类型为{} .",dataSourceName,DynamicDataSourceCache.DS_TYPE.get(DynamicDataSourceCache.CURRENT_DS).toLowerCase());
                dao = oracleGeneratorDao;
                for (String tableName : tableNames) {
                    //查询表信息
                    Map<String, String> table = queryTable(tableName, oracleGeneratorDao);
                    //查询列信息
                    List<Map<String, String>> columns = queryColumns(tableName, oracleGeneratorDao);
                    //生成代码
                    GenUtils.generatorCode(table, columns, zip);
                }
                break;
        }



        IOUtils.closeQuietly(zip);
        dataSourceService.removeCurrentDataSource();
        return outputStream.toByteArray();

    }
}
